//
//  VoiceChatContentsView.m
//  RainbowFM
//
//  Created by Kratos on 14/12/15.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "VoiceChatContentsView.h"

#define ScreenW [UIScreen mainScreen ].bounds.size.width
#define ScreenH [UIScreen mainScreen ].bounds.size.height

//主持人画面
#define DefineFrame CGRectMake(0,64.f,ScreenW,200.f);
#define DefineFullFrame CGRectMake(0,0,ScreenW,ScreenH);

#define DefineOffButtonFrame CGRectMake((ScreenW-310.f)/2,ScreenH - 43.f - 8.f,310.f,43.f);
#define DefineFullOffButtonFrame CGRectMake((ScreenW-64.f)/2,ScreenH - 67.f - 8.f,64.f,67.f);

#define DefineUserScreenFrame CGRectMake((ScreenW-124.f)/2,ScreenH - 43.f - 8.f * 2 - 124.f,124.f,124.f);
#define DefineFullUserScreenFrame CGRectMake(8.f,ScreenH -  8.f - 80.f,80.f,80.f);

#define DefineChatViewFrame CGRectMake(0,64.f,ScreenW,200.f);
#define DefineFullChatViewFrame CGRectMake(0,0,ScreenW,ScreenH);

#define DefineFullButtonFrame CGRectMake(ScreenW - 32.f - 8.f,225.f,32.f,32.f);
#define DefineFullFullButtonFrame CGRectMake(ScreenW - 32.f - 8.f,ScreenH -  8.f - 32.f,32.f,32.f);


@implementation VoiceChatContentsView

-(void)awakeFromNib
{
    _screenStatus = VoiceChatScreenStatusBan;
    
    _userView.layer.cornerRadius = CGRectGetWidth(_userView.bounds)/2.f;
    _userView.layer.masksToBounds = NO;
    _userView.clipsToBounds = YES;
    _userView.contentMode = UIViewContentModeScaleAspectFill;
    _userView.layer.borderWidth = 2.f;
    _userView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    UITapGestureRecognizer *oneTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(oneTapAction:)];
    oneTap.numberOfTapsRequired = 1;
    [_userView addGestureRecognizer:oneTap];
}

-(void)setScreenStatus:(VoiceChatScreenStatus)screenStatus
{
    if (_screenStatus == screenStatus) {
        return;
    }
    
    if (_isUserViewFull) {
        [self oneTapAction:nil];
    }
    
    _screenStatus = screenStatus;
    
    //主持人窗口
    CGRect frame;
    
    //挂断按钮
    NSString *offButtonImageNameN = @"";
    NSString *offButtonImageNameP = @"";
    
    //用户视频窗口
    CGRect offButtonFrame;
    CGRect userScreenFrame;
    
    //主持视频窗口
    CGRect chatViewFrame;
    CGRect fullButtonFrame;
    
    switch (_screenStatus) {
        case VoiceChatScreenStatusBan:
        {
            frame = DefineFrame;
            
            offButtonImageNameN = @"chat_b_off_n@3x.png";
            offButtonImageNameP = @"chat_b_off_p@3x.png";
            offButtonFrame = DefineOffButtonFrame;
            userScreenFrame = DefineUserScreenFrame;
            
            chatViewFrame = DefineChatViewFrame;
            fullButtonFrame = DefineFullButtonFrame;
        }
            break;
        case VoiceChatScreenStatusFull:
        {
            frame = DefineFullFrame;
            
            offButtonImageNameN = @"chat_f_off_n@3x.png";
            offButtonImageNameP = @"chat_f_off_p@3x.png";
            offButtonFrame = DefineFullOffButtonFrame;
            userScreenFrame = DefineFullUserScreenFrame;
            
            chatViewFrame = DefineFullChatViewFrame;
            fullButtonFrame = DefineFullFullButtonFrame;
        }
            break;
        default:
            break;
    }
    
    typeof(self) self_ = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self_) {
            if (self_.localVideoSurface) {
                self_.localVideoSurface.frame = CGRectMake(0, 0, CGRectGetWidth(userScreenFrame), CGRectGetHeight(userScreenFrame));
            }
            
            self_.chatView.frame = frame;
            
            [self_.takeOffButton setImage:[UIImage imageNamed:offButtonImageNameN] forState:UIControlStateNormal];
            [self_.takeOffButton setImage:[UIImage imageNamed:offButtonImageNameP] forState:UIControlStateHighlighted];
            self_.takeOffButton.frame = offButtonFrame;
            
            self_.userView.frame = userScreenFrame;
            if (screenStatus == VoiceChatScreenStatusBan) {
                self_.userView.layer.cornerRadius = CGRectGetWidth(userScreenFrame)/2.f;
                _userView.clipsToBounds = YES;
            }else{
                self_.userView.layer.cornerRadius = 0;
                _userView.clipsToBounds = NO;
            }
            
            self_.chatView.frame = chatViewFrame;
            self_.fullButton.frame = fullButtonFrame;
            
            [[NSNotificationCenter defaultCenter]postNotificationName:kRFMNotificationVoiceChatScreenChanged object:nil];
        }
    });
    
}

-(void)oneTapAction:(UITapGestureRecognizer *)recognizer
{
    if (_screenStatus == VoiceChatScreenStatusBan) {
        return;
    }

    if (_isUserViewFull) {
        _isUserViewFull = NO;
        typeof(self) self_ = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self_) {
                if (self_.localVideoSurface) {
                    self_.localVideoSurface.frame = CGRectMake(0, 0, 80.f, 80.f);
                }
                
                self_.userView.frame = DefineFullUserScreenFrame;
                self_.chatView.frame = DefineFullChatViewFrame;
                self_.chatView.layer.bounds = self_.chatView.bounds;
                
                [self_ bringSubviewToFront:self_.userView];
                [self_ sendSubviewToBack:self_.chatView];
                
                [[NSNotificationCenter defaultCenter]postNotificationName:kRFMNotificationVoiceChatScreenChanged object:nil];
            }
        });
    }else{
        _isUserViewFull = YES;
        typeof(self) self_ = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            if (self_) {
                if (self_.localVideoSurface) {
                    self_.localVideoSurface.frame = CGRectMake(0, 0,ScreenW, ScreenH);
                }
                
                self_.userView.frame = DefineFullChatViewFrame;
                self_.chatView.frame = DefineFullUserScreenFrame;
                self_.chatView.layer.bounds = self_.chatView.bounds;
                
                [self_ bringSubviewToFront:self_.chatView];
                [self_ sendSubviewToBack:self_.userView];
                
                [[NSNotificationCenter defaultCenter]postNotificationName:kRFMNotificationVoiceChatScreenChanged object:nil];
            }
        });
    }
}


//联通后回调触发
- (void) OnLocalVideoRelease:(id)sender
{
    if(_localVideoSurface) {
        _localVideoSurface = nil;
    }
}

- (void) OnLocalVideoInit:(id)session
{
    if(_localVideoSurface) {
        [_localVideoSurface removeFromSuperlayer];
        _localVideoSurface = nil;
    }
    
    _localVideoSurface = [AVCaptureVideoPreviewLayer layerWithSession: (AVCaptureSession*)session];
    _localVideoSurface.frame = _userView.bounds;
    _localVideoSurface.videoGravity = AVLayerVideoGravityResizeAspectFill;
    
    [_userView.layer addSublayer:_localVideoSurface];
}


-(IBAction)fullScreenAction:(id)sender
{
    VoiceChatScreenStatus status = _screenStatus == VoiceChatScreenStatusBan ? VoiceChatScreenStatusFull : VoiceChatScreenStatusBan;
    
    self.screenStatus = status;
    
    if (_fullBlock) {
        _fullBlock();
    }
}

-(IBAction)takeOffOnlineAction:(id)sender
{
    if (_takeOffBlock) {
        _takeOffBlock();
    }
}


@end
